/** The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License.
 *
 */
package tractorlk.drops;

import tractorlk.Drawable;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Polygon;
import java.awt.geom.Point2D.Float;
import javax.swing.ImageIcon;
import java.util.Random;


public class Card implements Drawable
{
    Image front;
    int posx;
    int posy;
    private boolean up;
    Polygon polygon;
    boolean isSet;

    private int rank;
    private int suit;
    private int id;
    private int gameID;
    Random rnd = new Random();

    public Card(int index)
    {
        super();
        id = index;
        gameID = -1;
        setSuit();
        setRank();
        this.polygon = new Polygon(new int[] { 0, 0, 75, 75 }, new int[] { 0, 100, 100, 0 }, 4);
        up = false;
        isSet = false;
        posx = 0;
        posy = 0;
        polygon.translate(0, 0);
        if(id >= 0)
        {
            java.net.URL imgURL = getClass().getResource("/deck/" + this.suit + "-" + this.rank + ".png");
            ImageIcon icon = new ImageIcon(imgURL);
            this.front = icon.getImage();
        }
    }

    public Card(Card c)
    {
        super();
        this.id = c.id;
        setSuit();
        setRank();
        this.polygon = new Polygon(new int[] { 0, 0, 75, 75 }, new int[] { 0, 100, 100, 0 }, 4);
        up = false;
        isSet = false;
        posx = 0;
        posy = 0;
        polygon.translate(0, 0);
        java.net.URL imgURL = getClass().getResource("/deck/" + this.suit + "-" + this.rank + ".png");
        ImageIcon icon = new ImageIcon(imgURL);
        this.front = icon.getImage();
        this.gameID = c.gameID;
    }

   

    public void dragged(int dx, int dy)
    {
        posx += dx;
        posy += dy;
    }

    public void setPosition(int x, int y)
    {
        if(!this.isSet)
        {
           // System.out.println("for: " + this + " moved from (" + posx +"," + posy + ") to (" + x + "," + y + ")");
            this.polygon.translate(x-posx, y-posy);
            posx = x;
            posy = y;
            isSet = true;
        }
    }

    public void unset()
    {
        this.isSet = false;
    }

    public void resetGameID()
    {
        this.gameID = -1;
    }

    public void clicked()
    {
       // System.out.println("The card: " + this.rank + " of " + this.suit + " was clicked");
       if(up == false)
       {
            posy -= 50;
            up = true;
            this.polygon.translate(0, -50);
       }
       else
       {
            posy += 50;
            up = false;
            this.polygon.translate(0, 50);
       }
    }

    public void down()
    {
       if(up == true)
       {
            posy += 50;
            up = false;
            this.polygon.translate(0, 50);
       }
    }

    public String toString()
    {
        String sRank = "??";
        if (this.rank == 16)
            sRank = "Red";
        else if (this.rank == 15)
            sRank = "Black";
        else if(this.rank == 14)
            sRank = "A";
        else if (this.rank == 13)
            sRank = "K";
        else if (this.rank == 12)
            sRank = "Q";
        else if (this.rank == 11)
            sRank = "J";
        else
            sRank = Integer.toString(this.rank);
        String sSuit = "??";
        if(this.suit == 1)
            sSuit = "C";
        else if (this.suit == 2)
            sSuit = "D";
        else if (this.suit == 3)
            sSuit = "H";
        else if (this.suit == 4)
            sSuit = "S";
        else if (this.suit == 5)
            sSuit = "Joker";

        return "[" + sRank + " - " + sSuit + "]";
    }

    public void draw(Graphics g)
    {
            //g.drawPolygon(this.polygon);
            //g.setColor(Color.red);
            //g.fillPolygon(this.polygon);
            g.drawImage(front,  posx,  posy, null);
    }

    private void setSuit()
    {
        int num = id%54;
        this.suit = (num/13+1);
    }

    private void setRank()
    {
        int num = id%54;
        if(num == 53)
            this.rank = 16; //red joker
        else if(num == 52)
            this.rank = 15; //black joker
        else
            this.rank = num%13+2;
    }

    public int getID()
    {
        return this.id;
    }

    public int getRank()
    {
       return this.rank;
    }

    public int getSuit()
    {
        return this.suit;
    }

    public boolean getStatus()
    {
        return this.up;
    }

    public int getGameID()
    {
        return this.gameID;
    }

    public void setGameID(int GID)
    {
        this.gameID = GID;
    }
}
